(function (vc) {

    vc.extends({
        data: {
            addPrinterMachineInfo: {
                machineId: '',
                machineCode: '',
                machineType: '',
                appId: '',
                appSecure: '',
                shopId: '',
                machineImpl: '',
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('addPrinterMachine', 'openAddPrinterMachineModal', function (_param) {
                $that.addPrinterMachineInfo.shopId = _param.shopId;
                $('#addPrinterMachineModel').modal('show');
            });
        },
        methods: {
            addPrinterMachineValidate() {
                return vc.validate.validate({
                    addPrinterMachineInfo: $that.addPrinterMachineInfo
                }, {
                    'addPrinterMachineInfo.machineCode': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设备编号不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "设备编号不能超过64"
                        },
                    ],
                    'addPrinterMachineInfo.machineType': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设备类型不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "设备类型不能超过64"
                        },
                    ],
                    'addPrinterMachineInfo.appId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "应用秘钥不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "应用秘钥不能超过64"
                        },
                    ],
                    'addPrinterMachineInfo.appSecure': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "秘钥不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "秘钥不能超过64"
                        },
                    ],
                    'addPrinterMachineInfo.shopId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商铺ID不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "商铺ID不能超过30"
                        },
                    ],
                    'addPrinterMachineInfo.machineImpl': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "设备厂家不能为空"
                        },
                    ],
                });
            },
            saveShopPrinterMachineInfo: function () {
                if (!$that.addPrinterMachineValidate()) {
                    vc.toast(vc.validate.errInfo);

                    return;
                }


                vc.http.apiPost(
                    '/printerMachine.saveShopPrinterMachine',
                    JSON.stringify($that.addPrinterMachineInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addPrinterMachineModel').modal('hide');
                            $that.clearAddPrinterMachineInfo();
                            vc.emit('printerMachine', 'listPrinterMachine', {});

                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            clearAddPrinterMachineInfo: function () {
                $that.addPrinterMachineInfo = {
                    machineCode: '',
                    machineType: '',
                    appId: '',
                    appSecure: '',
                    shopId: '',
                    machineImpl: '',

                };
            }
        }
    });

})(window.vc);
